import { BblAttach } from "../entity/attach.entity";

import * as path from "path";

export class AttachDto {
  constructor(att: BblAttach) {
    this.attId = att.attId;
    this.attName = att.attName;
    this.attExt = att.attExt;
    this.attState = att.attState;
    this.attMimeType = att.attMimeType;
    this.attStorageType = att.attStorageType;
    this.attCosKey = att.attCosKey;
    this.attStateText = att.attState === "1" ? "正式文件" : "临时文件";
    this.format(att);
  }

  attId: string;
  attName: string;
  /**
   * 附件扩展名，attStorageType = 1，为云存储文件时，保存该文件的key
   */
  attExt: string;
  /**
   * 存储类型: 0-本地文件, 1-云存储URL
   */
  attStorageType: string;
  attStorageTypeText: string;
  /**
   * 附件状态, 0: 临时, 1: 正式
   */
  attState: "0" | "1";
  attStateText: string;
  /**
   * 附件访问路径
   */
  url: string;
  attMimeType: string;

  /**
   * cos存储桶中的key
   */
  attCosKey: string;

  format(att: BblAttach) {
    if (att.attStorageType === "1") {
      this.url = att.attPath;
    } else {
      const src = this.attState === "0" ? att.attTempPath : att.attPath;
      const prefix = this.attState === "0" ? "/tmp/" : "/assets/";
      const filename = path.basename(src);

      this.url = prefix + filename;
    }

    if (this.attStorageType === "1") {
      this.attStorageTypeText = "云存储文件";
    } else {
      this.attStorageTypeText = "本地文件";
    }
  }
}
